# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

Main       <- read_dta("Data/Main.dta")
Main_Leave <- read_dta("Data/Main_Leave.dta")
Main       <- left_join(Main, Main_Leave, by = c("ID", "Date"))

summary(fit_logLateforWork   <-  felm(log(LateforWork + 1)   ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logLeaveEarly    <-  felm(log(LeaveEarly + 1)    ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logSickLeave     <-  felm(log(SickLeave + 1)     ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logPersonalLeave <-  felm(log(PersonalLeave + 1) ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))

summary(fit_logAttendanceTimePerWorkday <-  felm(log(AttendanceTimePerWorkday + 1) ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logTimeofArrival            <-  felm(log(TimeofArrival)                ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logTimeofLeaving            <-  felm(log(TimeofLeaving)                ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = Main, exactDOF = TRUE))
summary(fit_logbonusT                   <-  felm(LogBonusT                         ~ NearSubway:Post | ID + as.factor(Company):as.factor(Date) | 0 | ID + Date,  data = filter(Main, LeaveFlag == 1), exactDOF = TRUE))


Table3_Plain <- bind_rows(
  GenRegTable(fit = fit_logLateforWork,   depvar = "LateforWork",   type = "log1") ,
  GenRegTable(fit = fit_logLeaveEarly,    depvar = "LeaveEarly",    type = "log1"),
  GenRegTable(fit = fit_logSickLeave,     depvar = "SickLeave",     type = "log1"),
  GenRegTable(fit = fit_logPersonalLeave, depvar = "PersonalLeave", type = "log1"),
  
  GenRegTable(fit = fit_logAttendanceTimePerWorkday, depvar = "AttendanceTimePerWorkday", type = "log1",  Data = filter(Main, LeaveFlag == 1)),
  GenRegTable(fit = fit_logTimeofArrival,            depvar = "TimeofArrival",            type = "log",   Data = filter(Main, LeaveFlag == 1)),
  GenRegTable(fit = fit_logTimeofLeaving,            depvar = "TimeofLeaving",            type = "log",   Data = filter(Main, LeaveFlag == 1)),
  GenRegTable(fit = fit_logbonusT,                   depvar = "LogBonusT",                type = "level", Data = filter(Main, LeaveFlag == 1)))

Label1_DepVar <- c("Log(Late for ", "Log(Leave ", "Log(Sick ", "Log ",
                  "Log(Attendance ", "Log(Time of ", "Log(Time of ", "Log(Bonus) ")
Label2_DepVar <- c("Work)", "Early)", "Leave)", "(Personal ", "Time per ", "Arrival)", "Leaving)", "for attendance ")
Label3_DepVar <- c("", "", "", "Leave)", "Workday)", "", "", "sample")

## Produce latex table 
Table3 <- 
    TR("") %:% TR(seq(from = 1, to = 8, by = 1), se = TRUE, dec = 0) + 
  midrulep(list(c(2, 9))) +
    TR("") %:% TR(Label1_DepVar) +
    TR("") %:% TR(Label2_DepVar) +
    TR("") %:% TR(Label3_DepVar) +
  midrule() +
    TR("NearSubway $\\times$ Post") %:% with(Table3_Plain, TR(beta, pvalues = cpval, dec = 3)) +
    TR("")                          %:% with(Table3_Plain, TR(clse, se = TRUE, dec = 3)) +
  vspace(10) +
    TR("Adj. R-Squared")         %:% with(Table3_Plain, TR(adjR2,      dec = 3)) +
    TR("Obs")                    %:% with(Table3_Plain, TR(obs,        dec = 0)) +
    TR("N (affected group)")     %:% with(Table3_Plain, TR(N_affected, dec = 0)) +
    TR("N (control group)")      %:% with(Table3_Plain, TR(N_control,  dec = 0)) 

## Output files (.tex and .pdf)
TS(Table3, file = "Table3",
     pretty_rules = TRUE, header = c('l',rep('c',8)),
     output_path = paste0(getwd(), "/Output"))

## Open Table3.pdf with default PDF viewer
system2('open', args = 'Output/Table3.pdf', wait = FALSE)

## Remove intermediate products
rm(list = grep(pattern = "Main|fit|Label", ls(), value = TRUE))

